//
//  ZCWKWebViewConfiguration.m
//  DoctorOnline
//
//  Created by 赵隆杰 on 2018/8/20.
//  Copyright © 2018年 赵隆杰. All rights reserved.
//

#import "ZCWKWebViewConfiguration.h"

@interface ZCWKWebViewConfiguration()<WKScriptMessageHandler>

@end

@implementation ZCWKWebViewConfiguration

-(instancetype)init
{
    self = [super init];
    if (self) {
        [self config];
    }
    return self;
}
-(void)config
{
    self.preferences.javaScriptEnabled = YES;
    self.preferences.javaScriptCanOpenWindowsAutomatically = YES;
//    NSString *jSString = @"window.webkit.messageHandlers.AppNative.postMessage('')";
//    WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
    WKUserContentController *wkVC = [WKUserContentController new];
    [wkVC addScriptMessageHandler:self name:@"AppNative"];
//    [wkVC addUserScript:wkUserScript];
    
    
    self.userContentController = wkVC;
    
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    if ([message.name isEqualToString:@"AppNative"]) {
        NSString *bodyDataStr = [NSString stringWithFormat:@"%@",message.body];
        NSDictionary *params = bodyDataStr.mj_JSONObject;
        if (self.handleBlock) {
            self.handleBlock(params);
        }
    }
}

- (void)dealloc
{
    //    NSLog(@"HDJWKWebConfiguration - release");
}


@end
